package datadasetest;
import java.awt.EventQueue;

import javax.net.ssl.SSLEngineResult.Status;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class modi extends JInternalFrame {
	Connection conn;
	database db;
	Select s;
	modi m;
	int s1;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JButton button;
	private JLabel lblNewLabel_2;
	private JLabel 시리얼;
	private JTextField ss;

	public modi(	Connection conn,database db) {
		super("modi",true,true,false,false);
		setBounds(100, 100, 325, 235);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getButton());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(get시리얼());
		getContentPane().add(getSs());
this.conn=conn;
this.db=db;
	}
	public void setData(String a,String b, String c){
	a=code.getText();
	b=ea.getText();
	c=ss.getText();
		
	
}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(12, 22, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ea");
			lblNewLabel_1.setBounds(12, 47, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(53, 19, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(53, 47, 116, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				int s=Integer.parseInt(ss.getText());
				String sql="select * from ptbl where serial = ?";
				try{
					PreparedStatement pstmt= conn.prepareStatement(sql);
					pstmt.setInt(1, s);
					
					ResultSet rs= pstmt.executeQuery();
				if(rs.next()){//데이터가 있다면
					code.setText(rs.getString("code"));
					ea.setText(""+rs.getInt("ea"));//코드와 데이터 뿌려준다
				}else{
					JOptionPane.showMessageDialog(null, "데이터가 없습니다");
					code.setText("");
					ea.setText("");
					
				}
				rs.close();
				pstmt.close();
				}catch(Exception ex){
					ex.printStackTrace();
					
				}
				}
			});
			btnNewButton.setBounds(181, 18, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("수정");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				int s=Integer.parseInt(ss.getText());
				String c = code.getText();
				int e = Integer.parseInt(ea.getText());
				String sql = "update ptbl set code=?, ea=?"+"where serial=?";
				try{
					PreparedStatement pstmt = 
							conn.prepareStatement(sql);
					pstmt.setString(1, c);
					pstmt.setInt(2, e);
					pstmt.setInt(3, s);
					
					pstmt.executeUpdate();
					pstmt.close();
				}catch(Exception ex){
					ex.printStackTrace();
					
				}
						
				}
			});
			button.setBounds(53, 78, 116, 21);
		}
		return button;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("검색할 코드를 입력하세요....");
			lblNewLabel_2.setBounds(12, 161, 285, 15);
		}
		return lblNewLabel_2;
	}
	private JLabel get시리얼() {
		if (시리얼 == null) {
			시리얼 = new JLabel("시리얼");
			시리얼.setBounds(12, 123, 57, 15);
		}
		return 시리얼;
	}
	private JTextField getSs() {
		if (ss == null) {
			ss = new JTextField();
			ss.setBounds(81, 120, 116, 21);
			ss.setColumns(10);
		}
		return ss;
	}
}
